<?php
// $Id$

/**
 * Controller_Admin_User 控制器
 */
class Controller_Admin_Banner extends Controller_Abstract
{

	function actionIndex()
	{
        $banners = Banner::find()->order('position,sort')->getAll();
        $this->_view['banners'] = $banners;
	}

    function actionEdit(){
        $b = Banner::findById($this->_context->bid)->getOne();

        if($this->_context->isPost()){

            $b->changeProps($_POST);
            $b->link_target = isset($_POST['open_at_new_window'])?'_blank':'self';
            if(!isset($_POST['active'])) $b->active = 0;

            try{
                $b->save();
                $b->saveImg();

                return $this->_redirect(url('.', 'bid/'.$b->id()));

            }catch(QDB_ActiveRecord_ValidateFailedException  $e){
                $this->_view['errorMsg'] = '错误：'.$e->getMessage();
            }
        }

        $this->_view['b'] = $b;
        $this->_view['title'] = $b->id()?'编辑图片横幅':'添加图片横幅';
    }

    function actionCopy(){
        $b = Banner::findById($this->_context->bid)->getOne();
        $b->changePropForce('banner_id', null);
        $this->_view['b'] = $b;
        $this->_view['title'] = '复制图片横幅';
        $this->_forward('/edit');
    }

    function actionDel(){
        $b = Banner::findById($this->_context->bid)->getOne();
        if($b->id()) $b->destroy();
        return $this->_redirect(url('/'));
    }
}